﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.DAO.Admin;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.BO.Admin
{
    public class UserInGroupBO : BaseBO
    {
        #region Field list in database
        public const string FIELD_USER_IN_GROUP_ID = "UserInGroupId";
        public const string FIELD_USER_ID = "UserId";
        public const string FIELD_GROUP_ID = "GroupId";
        public const string FIELD_ISACTIVE = "IsActive";
        public const string FIELD_EMAIL = "Email";
        public const string FIELD_USER_NAME = "Username";
        public const string FIELD_FULL_NAME = "Fullname";
        public const string FIELD_GROUP_NAME = "Groupname";
        public const string FIELD_RECORD_COUNT = "RecordCount";
        public const string FIELD_USER_CODE = "UserCode";

        public string GroupCode { get; set; }
        #endregion
        
        private String _connectionString;

        public UserInGroupBO(string connString)
        {
            this._connectionString = connString;

        }

        public UserInGroupBO()
        {
            this._connectionString = Common.ConnectionUtil.DHQG_ADMIN_CONNECTION_STRING;

        }


        public UserInGroupDAO _UserInGroupDAO;

        public UserInGroupDAO CreateSQLDAO()
        {
            return new UserInGroupDAO(_connectionString, DEFAULT_MAPPER_TYPE);
        }

        public DataPage<UserInGroupModel> GetDynamicListPaging(UserInGroupModel aModel, int pageSize, int pageIndex, bool compareEqual)
        {
            try
            {
                _UserInGroupDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {UserInGroupModel.FIELD_USER_IN_GROUP_ID,FIELD_USER_IN_GROUP_ID},
                    {UserInGroupModel.FIELD_USER_ID,FIELD_USER_ID},
                    {UserInGroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},  
                    {UserInGroupModel.FIELD_USER_CODE,FIELD_USER_CODE},  
                    {UserInGroupModel.FIELD_USER_NAME,FIELD_USER_NAME},  
                    {UserInGroupModel.FIELD_GROUP_NAME,FIELD_GROUP_NAME},  
                    {UserInGroupModel.FIELD_EMAIL,FIELD_EMAIL},  
                    {UserInGroupModel.FIELD_FULL_NAME,FIELD_FULL_NAME},
                    {UserInGroupModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT}
                };
                return _UserInGroupDAO.GetDynamicListPaging(aModel, pageSize, pageIndex, mapDictionary, compareEqual);
            }
            catch (Exception)
            {
                throw;
            }
        }
        

        public DataPage<UserInGroupModel> GetDynamicListPaging_PM2(UserInGroupModel model, int pageSize, int pageIndex,bool compareEqual)
        {
            try
            {
                _UserInGroupDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {UserInGroupModel.FIELD_USER_IN_GROUP_ID,FIELD_USER_IN_GROUP_ID},
                    {UserInGroupModel.FIELD_USER_ID,FIELD_USER_ID},
                    {UserInGroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},  
                    {UserInGroupModel.FIELD_USER_CODE,FIELD_USER_CODE},  
                    {UserInGroupModel.FIELD_USER_NAME,FIELD_USER_NAME},  
                    {UserInGroupModel.FIELD_GROUP_NAME,FIELD_GROUP_NAME},  
                    {UserInGroupModel.FIELD_EMAIL,FIELD_EMAIL},  
                    {UserInGroupModel.FIELD_FULL_NAME,FIELD_FULL_NAME},
                    {UserInGroupModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT},
                    {UserInGroupModel.FIELD_ISACTIVE, FIELD_ISACTIVE}
                };
                return _UserInGroupDAO.GetDynamicListPaging_PM2(model, pageSize, pageIndex, mapDictionary, compareEqual);

            }
            catch
            {
                throw;
            }
        }

        public UserInGroupModel GetUserInGroupById(int? id)
        {
            try
            {
                _UserInGroupDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {UserInGroupModel.FIELD_USER_IN_GROUP_ID,FIELD_USER_IN_GROUP_ID},
                    {UserInGroupModel.FIELD_USER_ID,FIELD_USER_ID},
                    {UserInGroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},  
                   
                    {UserInGroupModel.FIELD_USER_NAME,FIELD_USER_NAME},  
                    {UserInGroupModel.FIELD_GROUP_NAME,FIELD_GROUP_NAME},  
                    {UserInGroupModel.FIELD_EMAIL,FIELD_EMAIL},  
                    {UserInGroupModel.FIELD_USER_CODE,FIELD_USER_CODE},  
                    {UserInGroupModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT}
                };

                return _UserInGroupDAO.GetById(id, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool createOrUpdateUserInGroup(UserInGroupModel model,int[] listID)
        {
            try
            {
                _UserInGroupDAO = CreateSQLDAO();
                return _UserInGroupDAO.createOrUpdateUserInGroup(model,listID);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool deleteUserInGroup(int[] ID)
        {
            try
            {
                _UserInGroupDAO = CreateSQLDAO();
                return _UserInGroupDAO.deleteUserInGroup(ID);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool InsertUserIntoGroup(USER_GROUP_CREATE us)
        {
            try
            {
                _UserInGroupDAO = CreateSQLDAO();
                return _UserInGroupDAO.InsertUserIntoGroup(us);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool AddUserIntoGroup(USER_GROUP_CREATE user)
        {
            try
            {
                _UserInGroupDAO = CreateSQLDAO();
                return _UserInGroupDAO.AddUserIntoGroup(user);
            }
            catch
            {
                throw;
            }
        }

        public List<UserInGroupModel> GetListUserInGroupByGroupId(int? groupId, int? AppId)
        {
            try
            {
                _UserInGroupDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {UserInGroupModel.FIELD_USER_IN_GROUP_ID,FIELD_USER_IN_GROUP_ID},
                    {UserInGroupModel.FIELD_USER_ID,FIELD_USER_ID},
                    {UserInGroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},  
                   
                    {UserInGroupModel.FIELD_USER_NAME,FIELD_USER_NAME},  
                    {UserInGroupModel.FIELD_GROUP_NAME,FIELD_GROUP_NAME},  
                    {UserInGroupModel.FIELD_EMAIL,FIELD_EMAIL},  
                    {UserInGroupModel.FIELD_USER_CODE,FIELD_USER_CODE},  
                    {UserInGroupModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT}
                };

                return _UserInGroupDAO.GetListUserInGroupByGroupId(groupId, AppId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool UserInGroupUpdate(UserInGroupUpdate us)
        {
            try
            {
                _UserInGroupDAO = CreateSQLDAO();
                return _UserInGroupDAO.UserInGroupUpdate(us);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool UpdateGroup(UserInGroupUpdate data)
        {
            try
            {
                _UserInGroupDAO = CreateSQLDAO();
                return _UserInGroupDAO.UpdateGroup(data);
            }
            catch 
            {
                throw;
            }
        }
    }
}